package com.woniu40.petai_manager.web;

import com.woniu40.petai_manager.entity.Brand;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("brand")
@RestController
@Api("品牌类")
@CrossOrigin
public class BrandController {

    @Autowired
    BrandService brandService;

    @PostMapping("/add")
    @ApiOperation("新增品牌")
    public ResponseResult<String> add(@RequestBody Brand brand){
        ResponseResult<String> result = new ResponseResult<>();
        int i = brandService.addBrand(brand);
        result.setCode(200);
        result.setMessage("添加品牌成功！");
        return result;
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除品牌")
    public ResponseResult<String> remove(Integer brandId){
        ResponseResult<String> result = new ResponseResult<>();
        int i = brandService.removeBrand(brandId);
        result.setCode(200);
        result.setMessage("删除品牌成功！");
        return result;
    }

    @GetMapping("/findById")
    @ApiOperation("根据ID查询品牌对象")
    public ResponseResult<Brand> toModify(Integer brandId){
        ResponseResult<Brand> result = new ResponseResult<>();
        Brand brand = brandService.findById(brandId);
        result.setCode(200);
        result.setMessage("查询品牌成功！");
        result.setData(brand);
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation("更新品牌信息")
    public ResponseResult<String> modify(@RequestBody Brand brand){
        ResponseResult<String> result = new ResponseResult<>();
        int i = brandService.modifyBrand(brand);
        result.setCode(200);
        result.setMessage("更新成功！");
        return result;
    }

    @DeleteMapping("/removeAll")
    @ApiOperation("根据id删除品牌(可批量)")
    public ResponseResult<String> removeGoods(String ids){
        ResponseResult<String> result = new ResponseResult<>();
        if(ids.contains(",")){
            List<Integer> idList = new ArrayList<>();
            String[] idsStr = ids.split(",");
            for (String idStr : idsStr) {
                idList.add(Integer.valueOf(idStr));
            }
            int i = brandService.removeByIds(idList);
        }else{
            Integer id = Integer.valueOf(ids);
            int i = brandService.removeBrand(id);
        }
        result.setCode(200);
        result.setMessage("删除品牌成功！");
        return result;
    }

}
